package com.zzyl.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.domain.vo.NursingProjectVo;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理项目Service业务层处理
 *
 * @author mr.Li
 * @date 2025-05-14
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        // 删除缓存数据
        removeCache();
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        // 删除缓存数据
        removeCache();
        return removeBatchByIds(Arrays.asList(ids)) ? ids.length : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        // 删除缓存数据
        removeCache();
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询全部护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        // 1.首先查询缓存中是否存在
        List<String> stringJSON = redisTemplate.opsForList().range(CacheConstants.NURSING_PROJECT_LIST_KEY, 0, -1);

        // 2.如果存在，则直接返回
        List<NursingProjectVo> projectList = null;
        if (CollUtil.isNotEmpty(stringJSON)) {
            // 2.1如果存在，则返回前端数据
            projectList = stringJSON.stream().map(x -> {
                // 2.2将JSON字符串转为对象
                return JSON.parseObject(x, NursingProjectVo.class);
            }).collect(Collectors.toList());
        }

        // 3.如果不存在则查询数据库
        if (CollUtil.isEmpty(projectList)) {
            //select name , id  from nursing_project where status=1
            List<NursingProject> nursingProjectList = this.lambdaQuery().eq(NursingProject::getStatus, 1)
                    .select(NursingProject::getName, NursingProject::getId)
                    .list();

            // 3.1将数据转为前端需要的数据
            projectList = nursingProjectList.stream().map(p -> {
                // 3.2将数据保存到缓存中
                redisTemplate.opsForList().leftPush(CacheConstants.NURSING_PROJECT_LIST_KEY, JSON.toJSONString(p));
                // 3.3将数据转为前端需要的数据
                return NursingProjectVo.builder().label(p.getName()).value(p.getId().toString()).build();
            }).collect(Collectors.toList());
        }

        // 4.返回数据
        return projectList;
    }

    /**
     * 删除缓存
     */
    private void removeCache() {
        // 1.查询缓存中是否存在
        List<String> range = redisTemplate.opsForList().range(CacheConstants.NURSING_PROJECT_LIST_KEY, 0, -1);
        if (CollUtil.isNotEmpty(range)) {
            // 2.不为空删除缓存
            redisTemplate.delete(CacheConstants.NURSING_PROJECT_LIST_KEY);
        }
    }
}